<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <base target="_self">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache,must-ridate">
    <meta http-equiv="expires" content="0">
    <title></title>
    <link rel="stylesheet" type="text/css" href="/static/plugins/bootstrap/css/bootstrap.min.css" />
    <link rel="stylesheet" type="text/css" href="/static/plugins/bootstrap-table/bootstrap-table.css" />
    <script type="text/javascript" src="/static/plugins/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/plugins/bootstrap-table/bootstrap-table.js"></script>
    <script type="text/javascript" src="/static/plugins/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
</head>

<style  type="text/css">
    .required {
        color: red;
    }       
</style>

<body>
    <div style="text-align: right;">
        <button type="button" id="btnSave" class="btn btn-danger">保 存</button>&nbsp;&nbsp;
    </div>
    <div id="jydsysInfo">
                    
    </div>
    <div style="height: 10px;"></div>
    <div style="width: 100%; height: 30px; text-align: center; line-height: 30px; background-color: #C0C0C0;">
        <a href="#" onclick="addJydsys(0,'','','');">点击添加更多交易对手</a>
    </div>
</body>
    
    <script type="text/javascript">
        //初始化数据
        let ydjData = new Object();
        $(function () {
            let uuid = GetQueryValue('uuid');
            let bsid = GetQueryValue('bsid');
            let regitem_id = GetQueryValue('regitem_id');
            let productid = GetQueryValue('productid');
            let productcode = GetQueryValue('productcode');

            //加载交易对手
            find_app_dfs_zxd_csjyds_by_uuid(uuid);

            //保存信托合同信息
            $("#btnSave").click(function() {
				//保存之前必输项校验
				let value=requiredCheck();
				if(value=="0"){		
					saveJydsys(uuid, bsid, regitem_id, productid, productcode);
				}
            });
        });

        //查询预登记-交易对手要素
        function find_app_dfs_zxd_csjyds_by_uuid(uuid) {
            jQuery.ajax({
                data: { "uuid": uuid },
                url: "/node/find_app_dfs_zxd_csjyds_by_uuid/" + uuid,
                async: false,
                cache: false,
                success: function (data) {
                    if (typeof(data.length) != "undefined") {
                        $.each(data, function (index, item) {
                            addJydsys(item.id, item.jydsjgzzjgdm, item.jydsjglx, item.jydsjgzclb);
                        });
                    }
                }
            });
        }

        //添加交易对手要素
        var q = 1;
        function addJydsys(id, jydsjgzzjgdm, jydsjglx, jydsjgzclb) {
            let str = `<div id="jydsysDiv` + q + `"><div style="height: 10px;"></div>
                    <input type="hidden" name="jydsysNo`+ q + `" value="` + q + `">
                    <input type="hidden" id="id`+ q + `" name="id` + q + `" value="` + id + `">
                    <table border="1" bordercolor="#C0C0C0" cellspacing="2" cellpadding="2"
                        style="width: 100%;">
                        <tr>
                            <td>
                                交易对手机构统一社会信用代码:
                            </td>
                            <td>
                                <input type="text" id="jydsjgzzjgdm`+ q + `" name="jydsjgzzjgdm` + q + `" value="` + jydsjgzzjgdm + `" style="width: 180px;">
                            </td>
                            <td>
                                交易对手机构类型：
                            </td>
                            <td>
                                <select id="jydsjglx`+ q + `" name="jydsjglx` + q + `" style="width: 180px;">

                                </select>
                            </td>
                            <td>
                                交易对手机构注册类别：
                            </td>
                            <td>
                                <select id="jydsjgzclb`+ q + `" name="jydsjgzclb` + q + `" style="width: 180px;">

                                </select>
                            </td>
                            <td style="width: 3%;">
                                <a href="#" onclick="delJydsys(`+ q + `);"><font color="#FF0000">删除</font></a>
                            </td>
                        </tr>
                    </table></div>`;
            $("#jydsysInfo").append(str);
            
            initSelect("jydsjglx" + q, "Init047", jydsjglx);
            initSelect("jydsjgzclb" + q, "Init048", jydsjgzclb);

            q++;
        }

        //删除交易对手要素
        function delJydsys(no) {
			if (confirm("确定执行交易对手删除操作吗？"))
			{		
				let id = $("#id" + no).val();
				if (id != 0) {
					let tjdNos = new Object();
					tjdNos.id = id;
					jQuery.ajax({
						//请求方式
						type: "POST",
						//请求地址
						url: "/node/delete_app_dfs_zxd_csjyds",
						//数据，json对象
						data: tjdNos,
						//请求成功
						success: function (data) {
							if (data.code == "1") {
								$("#jydsysDiv" + no).remove();
								alert("交易对手删除成功！");
							} else if (data.code == "3") {
								alert("交易对手ID为空！");
							} else {
								alert("交易对手删除失败！");
							}
						},
						//请求失败，包含具体的错误信息
						error: function (e) {
							alert("数据请求异常！");
						}
					});
				} else {
					$("#jydsysDiv" + no).remove();
				}
			}
        }
		//保存之前必输项校验
		function requiredCheck(){
			let value = "0";	
            $("input[name^='jydsysNo']").each(function name(params) {
                let no = $(this).val();
				let tjdNos = new Object();
				tjdNos.jydsjgzzjgdm = $("#jydsjgzzjgdm" + no).val();		
				if(typeof(tjdNos.jydsjgzzjgdm)=="undefined" || tjdNos.jydsjgzzjgdm.length==0  || tjdNos.jydsjgzzjgdm.length=='')
				{
					alert("字段[交易对手机构统一社会信用代码]为必填项!");
					value="1";
				}
			});
			
			return value;
		}	
        //保存预登记-交易对手要素
        function saveJydsys(uuid, bsid, regitem_id, productid, productcode) {
            let code = "";		
            $("input[name^='jydsysNo']").each(function name(params) {
                let no = $(this).val();
                let tjdNos = new Object();
                tjdNos.id = $("#id" + no).val();
                tjdNos.uuid = uuid;
                tjdNos.bsid = bsid;
                tjdNos.regitem_id = regitem_id;
                tjdNos.productid = productid;
                tjdNos.productcode = productcode;
                tjdNos.jydsjgzzjgdm = $("#jydsjgzzjgdm" + no).val();				
                tjdNos.jydsjglx = $("#jydsjglx" + no).val();
                tjdNos.jydsjgzclb = $("#jydsjgzclb" + no).val();
                jQuery.ajax({
                    //请求方式
                    type: "POST",
                    //请求地址
                    url: "/node/insert_app_dfs_zxd_csjyds",
                    //数据，json对象
                    data: tjdNos,
                    //请求成功
                    success: function (data) {
                        if (data.code != code) {
                            if (data.code == "1") {
                                alert("交易对手保存成功！");
                            } else {
                                alert("交易对手保存失败！");
                            }
                        }
                        code = data.code;
                    },
                    //请求失败，包含具体的错误信息
                    error: function (e) {
                        alert("数据请求异常！");
                    }
                });
            });
        }

        //初始化数据字典
        function initSelect(id, region_code, val) {
            jQuery.ajax({
                //请求方式
                type: "POST",
                //请求地址
                url: "/node/query_app_dfs_scode_content",
                //数据，json对象
                data: { "region_code": region_code, "item_code": "" },
                success: function (data) {
                    if (data != "" && data.length != 1) {
                        $.each(data, function (index, item) {
                            if (item.ITEM_CODE == val) {
                                $("#" + id).append("<option value='" + item.ITEM_CODE + "' selected>" + item.ITEM_NAME + "</option>");
                            } else {
                                $("#" + id).append("<option value='" + item.ITEM_CODE + "'>" + item.ITEM_NAME + "</option>");
                            }
                        });
                    }
                }
            });
        }
    
        //获取GET参数
        function GetQueryValue(queryName) {
            var query = decodeURI(window.location.search.substring(1));
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == queryName) { return pair[1]; }
            }
            return null;
        }
    </script>
</html>